# -*- coding: utf-8 -*-
'''
     @Author : LiWenqian
     @File : load.py
     @Project: scpythonProject
     @CreateTime : 2022/8/11 18:49:50
'''
import requests, re, time
from threading import Thread
from multiprocessing import Pool

def acquire_img(url):
    response = requests.get(url)
    response.encoding = 'utf-8'
    content = response.text
    # 获取图片名称
    img_lst = re.findall(r"(?<=<img src=).*(?= width)", content)
    return img_lst

def load_img(url, img_url):
    # 获取图片
    img_content = requests.get(url+img_url)
    img_content.encoding = 'utf-8'
    # 保存图片
    with open(f'./img/{img_url}', 'wb+') as fp:
        fp.write(img_content.content)

def main():
    url = "https://www.sanchuangedu.cn/"
    img_lst = acquire_img(url)
    t_lst = []
    # 使用多线程下载图片
    for img_url in img_lst:
        t = Thread(target=load_img, args=(url, img_url))
        t_lst.append(t)
        t.start()
    for t in t_lst:
        t.join()

    # 直接下载，大约需要22s
    # for img_url in img_lst:
    #     load_img(url, img_url)

    # 使用进程池
    # p = Pool(4)
    # for img_url in img_lst:
    #     p.apply_async(func=load_img, args=(url, img_url))
    # p.close()
    # p.join()

if __name__ == "__main__":
    start = time.time()
    main()
    end = time.time()
    print(end-start)